package com.sakiko.audiobook.album.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sakiko.audiobook.album.service.TrackInfoService;
import com.sakiko.audiobook.album.service.VodService;
import com.sakiko.audiobook.common.login.SakiLogin;
import com.sakiko.audiobook.common.result.Result;
import com.sakiko.audiobook.common.util.AuthContextHolder;
import com.sakiko.audiobook.model.album.TrackInfo;
import com.sakiko.audiobook.query.album.TrackInfoQuery;
import com.sakiko.audiobook.vo.album.AlbumTrackListVo;
import com.sakiko.audiobook.vo.album.TrackInfoVo;
import com.sakiko.audiobook.vo.album.TrackListVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired // 云点播服务 (腾讯云 https://cloud.tencent.com/document/api/266/31752
    private VodService vodService;

    /**
     * 批量获取下单付费声音列表
     *
     * @param trackId
     * @param trackCount
     * @return
     */
    @GetMapping("/findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable("trackId") Long trackId, @PathVariable("trackCount") Integer trackCount) {
        List<TrackInfo> list = trackInfoService.findPaidTrackInfoList(trackId, trackCount);
        return Result.ok(list);
    }

    /**
     * 查询用户分集购买列表
     *
     * @param trackId
     * @return
     */
    @Operation(summary = "查询用户分集购买列表")
    @GetMapping("/findUserTrackPaidList/{trackId}")
    public Result findUserTrackPaidList(@PathVariable Long trackId) {
        // 获取用户 id ==> 用户购买的 ==> 使用远程 user_paid_track;
        // 调用服务
        List<Map<String, Object>> list = trackInfoService.findUserTrackPaidList(trackId);
        // 返回数据
        return Result.ok(list);
    }


    /**
     * 根据专辑获取声音列表
     *
     * @param albumId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @SakiLogin(required = false)
    @Operation(summary = "根据专辑获取声音列表")
    @GetMapping("/findAlbumTrackPage/{albumId}/{pageNo}/{pageSize}")
    public Result findAlbumTrackPage(@PathVariable Long albumId, @PathVariable Long pageNo, @PathVariable Long pageSize) {
        // 构建分页对象
        Page<AlbumTrackListVo> trackListVoPage = new Page<>(pageNo, pageSize);
        // 获取用户 id
        Long userId = AuthContextHolder.getUserId() == null ? 1L : AuthContextHolder.getUserId();
        // 调用服务层方法
        IPage<AlbumTrackListVo> iPage = trackInfoService.findAlbumTrackPage(trackListVoPage, albumId, userId);
        // 返回数据
        return Result.ok(iPage);
    }

    /**
     * 修改声音
     *
     * @param trackId
     * @param trackInfoVo
     * @return
     */
    @Operation(summary = "修改声音")
    @PutMapping("/updateTrackInfo/{trackId}")
    public Result updateTrackInfo(@PathVariable Long trackId, @RequestBody @Validated TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(trackId, trackInfoVo);
        return Result.ok();
    }

    /**
     * 根据声音 id获取声音对象
     *
     * @param trackId
     * @return
     */
    @Operation(summary = "根据声音id获取声音对象")
    @GetMapping("/getTrackInfo/{trackId}")
    public Result getTrackInfo(@PathVariable Long trackId) {
        TrackInfo trackInfo = trackInfoService.getTrackInfo(trackId);
        return Result.ok(trackInfo);
    }

    /**
     * 根据声音 Id删除声音对象
     *
     * @param trackId
     * @return 需要根据保存逆向操作，而不简单的调用 .deleteById()
     */
    @Operation(summary = "根据声音Id删除声音对象")
    @DeleteMapping("/removeTrackInfo/{trackId}")
    public Result removeTrackInfo(@PathVariable("trackId") Long trackId) {
        trackInfoService.removeTrackInfo(trackId);
        return Result.ok();
    }

    /**
     * 分页查询用户声音列表
     *
     * @param pageNo
     * @param pageSize
     * @param trackInfoQuery
     * @return
     */
    @SakiLogin
    @Operation(summary = "查看声音列表")
    @PostMapping("/findUserTrackPage/{pageNo}/{pageSize}")
    public Result findUserTrackPage(@PathVariable Long pageNo, @PathVariable Long pageSize, @RequestBody TrackInfoQuery trackInfoQuery) {
        // 获取用户 id
        Long userId = AuthContextHolder.getUserId() == null ? 1L : AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        // 构建分页条件对象
        Page<TrackListVo> trackListVoPage = new Page<>(pageNo, pageSize);
        // 调用服务
        IPage<TrackListVo> iPage = trackInfoService.findUserTrackPage(trackListVoPage, trackInfoQuery);
        // 返回数据
        return Result.ok(iPage);
    }

    /**
     * 上传声音
     *
     * @param file
     * @return
     */
    @Operation(summary = "上传声音")
    @PostMapping("/uploadTrack")
    public Result uploadTrack(MultipartFile file) {
        Map<String, Object> map = vodService.uploadTrack(file);
        return Result.ok(map);
    }

    /**
     * 保存声音
     *
     * @return
     */
    @SakiLogin
    @Operation(summary = "保存声音")
    @PostMapping("/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
        // 上传声音的时候，用户也应当是登陆状态
        Long userId = AuthContextHolder.getUserId() == null ? 1L : AuthContextHolder.getUserId();
        trackInfoService.saveTrackInfo(trackInfoVo, userId);
        return Result.ok();
    }
}

